<template>
	<view class="u-tips" @click="goChat" :class="[isShow ? 'u-tip-show' : '']" :style="{
		top: navbarHeight + 'px',
		zIndex: uZIndex
	}">
		<view class="talk-item-view">
			<view class="talk-img">
				<u-avatar :src="noticeMsg.face" :text="noticeMsg.face ? '' : noticeMsg.name"
					bg-color="#DDDDDD"></u-avatar>
			</view>
			<view class="talk-info">
				<view class="talk-info-top">
					<view class="talk-name u-line-1">{{ noticeMsg.name }} </view>
					<view class="talk-time">
						{{ beautifyTime(noticeMsg.lastTalkTime) }}
					</view>
				</view>
				<view class="talk-message u-line-1">
					<view class="talk-message u-line-1" v-if="noticeMsg.lastMessageType == 'MESSAGE'">
						{{ noticeMsg.lastTalkMessage||'' }}
					</view>
					<view class="talk-message u-line-1" v-else-if="noticeMsg.lastMessageType == 'PICTURE'">[图片]
					</view>
					<view class="talk-message u-line-1" v-else-if="noticeMsg.lastMessageType == 'VIDEO'">[视频]
					</view>
					<view class="talk-message u-line-1" v-else-if="noticeMsg.lastMessageType == 'VOICE'">[语音]
					</view>
					<view class="talk-message u-line-1" v-else></view>
				</view>
			</view>

		</view>

	</view>
</template>

<script>
	/**
	 * topTips 顶部提示
	 * @description 该组件一般用于页面顶部向下滑出一个提示，尔后自动收起的场景。
	 * @tutorial https://www.uviewui.com/components/topTips.html
	 * @property {String Number} navbar-height 导航栏高度(包含状态栏高度在内)，单位PX
	 * @property {String Number} z-index z-index值（默认975）
	 * @example <u-top-tips ref="uTips"></u-top-tips>
	 */
	import {
		beautifyTime
	} from "@/utils/filters.js";
	export default {
		name: "u-top-tips",
		props: {
			// 导航栏高度，用于提示的初始化
			navbarHeight: {
				type: [Number, String],
				// #ifndef H5
				default: 0,
				// #endif
				// #ifdef H5
				default: 44,
				// #endif
			},
			// z-index值
			zIndex: {
				type: [Number, String],
				default: ''
			}
		},
		data() {
			return {
				timer: null, // 定时器
				isShow: false, // 是否显示消息组件
				duration: 10000, // 组件显示的时间，单位为毫秒
				noticeMsg: {
					// chatId: result.id,
					// face: result.face2,
					// lastMessageType: result.lastMessageType,
					// lastTalkMessage: result.lastTalkMessage,
					// lastTalkTime: result.lastTalkTime,
					// name: result.name2,
				}
			};
		},
		computed: {
			uZIndex() {
				return this.zIndex ? this.zIndex : this.$u.zIndex.topTips;
			}
		},
		methods: {
			beautifyTime,
			show(config = {}) {
				// 先清除定时器（可能是上一次定义的，需要清除了再开始新的）
				clearTimeout(this.timer);
				// 时间，内容，类型主题(type)等参数
				if (config.duration) this.duration = config.duration;
				this.noticeMsg = config;
				setTimeout(()=> {
					this.isShow = true;
					// 倒计时
					this.timer = setTimeout(() => {
						this.isShow = false;
						clearTimeout(this.timer);
						this.timer = null;
					}, this.duration);
				}, 10);
			},
			goChat(){
				let payload = {
				  gameId: this.noticeMsg.gameId,
				  storyId: this.noticeMsg.storyId,
				  chatId: this.noticeMsg.chatId,
				};
				this.$options.filters.talkIm(payload);
			},
		}
	};
</script>

<style lang="scss" scoped>
	view {
		box-sizing: border-box;
	}

	// 顶部弹出类型样式
	.u-tips {
		width: 100%;
		position: fixed;
		z-index: 1;
		padding: 20rpx 30rpx;
		color: #FFFFFF;
		font-size: 28rpx;
		left: 0;
		right: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		opacity: 0;
		// 此处为最核心点，translateY(-100%)意味着将其从Y轴隐藏（隐藏到顶部(h5)或者说导航栏(app)下面）
		transform: translateY(-100%);
		transition: all 0.35s linear;
	}

	.u-tip-show {
		transform: translateY(0);
		opacity: 1;
		z-index: 99;
	}
	
	.talk-item-view {
		
		border-radius: 28rpx;
		background-color: #fff;
			margin: 36rpx 44rpx 20rpx 44rpx;
		
	  display: flex;
	  padding: 26rpx;
		
	  .talk-img {
	    width: 92rpx;
	    height: 92rpx;
	    margin-right: 24rpx;
		  position: relative;
	  }
	
	  .talk-info {
	    flex: 1;
		  display: flex;
		  flex-direction: column;
		  .talk-info-top{
			  display: flex;
			  .talk-name {
			  	width:calc(100vw - 420rpx);
			    margin-bottom: 8rpx;
			  		font-size: 32rpx;
			  		font-family: PingFangSC, PingFang SC;
			  		font-weight: 500;
			  		color: #181818;
			  }
			  
			  .talk-time {
				  text-align: right;
				  flex:1;
			    position: relative;
			    font-size: 26rpx;
			    font-family: PingFangSC, PingFang SC;
			    font-weight: 400;
			    color: #CDCDCD;
			  }
		  }
	
	
	    .talk-message {
			width: calc(100vw - 400rpx);
			font-size: 28rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 300;
			color: #979797;
	    }
	  }
	}
	
</style>